import { getToken, removeToken } from '@/utils/auth'
import { setToken } from '@/utils/auth'
import { login, getProfile } from '@/api/user.js'
import { Message } from 'element-ui'
const state = {
  token: getToken() || null,
  userInfo: {}
}

const mutations = {
  setToken(state, value) {
    state.token = value
  },
  setUserInfo(state, value) {
    state.userInfo = value
  },
  logout(state, value) {
    state.token = ''
    removeToken()
    state.userInfo = {}
  }
}

const actions = {
  // user login
  async doLogin(context, { mobile, password }) {
    const res = await login({ mobile, password })
    setToken(res.data)
    context.commit('setToken', res.data)
    Message.success('登录成功')
  },
  async getUser(context, value) {
    const res = await getProfile()
    context.commit('setUserInfo', res.data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

